<template>
    <main class="main-page login-page">
        <div class="login-background">
            <div id="particles-js"></div>
            <div class="animated-shapes">
                <div class="shape shape1"></div>
                <div class="shape shape2"></div>
                <div class="shape shape3"></div>
            </div>
        </div>
        <section class="page-section">
            <div class="container-fluid">
                <div class="grid justify-content-center">
                    <div class="col-12 sm:col-6 md:col-4 comp-grid">
                        <div class="login-card">
                            <div class="login-header">
                                <div class="logo-icon">企</div>
                                <h1 class="text-2xl font-bold text-primary mt-3">企业API申请</h1>
                                <p class="text-500 mt-2">区块链教育认证平台</p>
                            </div>
                            <div class="login-form">
                                <div class="status-tip mb-3">
                                    <i class="pi pi-info-circle mr-2"></i>
                                    <span>请先输入营业执照号查询申请状态</span>
                                </div>
                                <form ref="observer" tag="form" @submit.prevent="submitForm()">
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-building"></i>
                                        <InputText 
                                            :label="$t('companyName')" 
                                            :placeholder="$t('companyName')" 
                                            class="w-full" 
                                            v-model.trim="formData.company_name" 
                                            required="required" 
                                            type="text" 
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-id-card"></i>
                                        <InputText 
                                            :label="$t('businessLicense')" 
                                            :placeholder="$t('businessLicense')" 
                                            class="w-full" 
                                            v-model.trim="formData.business_license" 
                                            required="required" 
                                            type="text" 
                                        />
                                        <Button 
                                            v-if="formData.business_license"
                                            icon="pi pi-search" 
                                            :label="$t('checkStatus')"
                                            class="p-button-info p-button-rounded check-status-btn" 
                                            @click="checkStatus"
                                            :loading="statusLoading"
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-user"></i>
                                        <InputText 
                                            :label="$t('contactPerson')" 
                                            :placeholder="$t('contactPerson')" 
                                            class="w-full" 
                                            v-model.trim="formData.contact_person" 
                                            required="required" 
                                            type="text" 
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-phone"></i>
                                        <InputText 
                                            :label="$t('contactPhone')" 
                                            :placeholder="$t('contactPhone')" 
                                            class="w-full" 
                                            v-model.trim="formData.contact_phone" 
                                            required="required" 
                                            type="text" 
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-envelope"></i>
                                        <InputText 
                                            :label="$t('contactEmail')" 
                                            :placeholder="$t('contactEmail')" 
                                            class="w-full" 
                                            v-model.trim="formData.contact_email" 
                                            required="required" 
                                            type="email" 
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-map-marker"></i>
                                        <InputText 
                                            :label="$t('companyAddress')" 
                                            :placeholder="$t('companyAddress')" 
                                            class="w-full" 
                                            v-model.trim="formData.company_address" 
                                            required="required" 
                                            type="text" 
                                        />
                                    </div>
                                    <div class="mb-2 p-input-icon-right w-full">
                                        <i class="pi pi-globe"></i>
                                        <InputText 
                                            :label="$t('companyWebsite')" 
                                            :placeholder="$t('companyWebsite')" 
                                            class="w-full" 
                                            v-model.trim="formData.company_website" 
                                            type="text" 
                                        />
                                    </div>
                                    <Message v-if="errorMsg" severity="error" :closable="true" @close="errorMsg=null">
                                        {{ errorMsg.toString() }}
                                    </Message>
                                    <div v-if="applicationStatus" class="mb-3">
                                        <Card>
                                            <template #title>
                                                <div class="flex align-items-center">
                                                    <i class="pi pi-building mr-2"></i>
                                                    {{ applicationStatus.company_name }}
                                                </div>
                                            </template>
                                            <template #content>
                                                <div class="grid">
                                                    <div class="col-12">
                                                        <div class="flex align-items-center mb-3">
                                                            <i class="pi pi-info-circle mr-2"></i>
                                                            <span class="font-bold">申请状态：</span>
                                                            <Tag 
                                                                :severity="getStatusSeverity(applicationStatus.application_status)"
                                                                :value="getStatusText(applicationStatus.application_status)"
                                                                class="ml-2"
                                                            />
                                                        </div>
                                                        <div class="flex align-items-center mb-3">
                                                            <i class="pi pi-calendar mr-2"></i>
                                                            <span class="font-bold">申请时间：</span>
                                                            <span class="ml-2">{{ formatDate(applicationStatus.created_at) }}</span>
                                                        </div>
                                                        <div class="flex align-items-center mb-3">
                                                            <i class="pi pi-clock mr-2"></i>
                                                            <span class="font-bold">更新时间：</span>
                                                            <span class="ml-2">{{ formatDate(applicationStatus.updated_at) }}</span>
                                                        </div>
                                                        <div v-if="applicationStatus.reviewer_notes" class="flex align-items-start mb-3">
                                                            <i class="pi pi-comment mr-2"></i>
                                                            <span class="font-bold">审核备注：</span>
                                                            <span class="ml-2">{{ applicationStatus.reviewer_notes }}</span>
                                                        </div>
                                                    </div>
                                                </div>
                                            </template>
                                        </Card>
                                    </div>
                                    <div class="text-center">
                                        <Button 
                                            :label="$t('submit')" 
                                            :loading="loading" 
                                            icon="pi pi-send" 
                                            class="p-button-lg p-button-raised w-full" 
                                            type="submit"
                                        >
                                        </Button>
                                    </div>
                                </form>
                            </div>
                            <div class="login-footer">
                                <div class="text-center">
                                    <router-link to="/">
                                        <Button icon="pi pi-arrow-left" :label="$t('backToLogin')" class="p-button-text" />
                                    </router-link>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useApp } from 'src/composables/app';
import { useRouter } from 'vue-router';
import { ApiService } from 'src/services/api';
import 'particles.js';

const router = useRouter();
const app = useApp();
const formData = reactive({
    company_name: '',
    business_license: '',
    contact_person: '',
    contact_phone: '',
    contact_email: '',
    company_address: '',
    company_website: ''
});

const loading = ref(false);
const errorMsg = ref('');
const statusLoading = ref(false);
const applicationStatus = ref(null);

async function checkStatus() {
    try {
        statusLoading.value = true;
        applicationStatus.value = null;
        errorMsg.value = '';

        const response = await ApiService.get(`employer/check-status/${formData.business_license}`);
        console.log('Status check response:', response);
        if (response.data.success) {
            errorMsg.value = '';
            console.log('Status check success:', response.data);
            applicationStatus.value = response.data;
        } else {
            errorMsg.value = response.data.message || '查询失败，请稍后重试';
        }
    } catch (error) {
        console.error('Status check error:', error);
        errorMsg.value = error.response?.data?.message || '查询失败，请稍后重试';
    } finally {
        statusLoading.value = false;
    }
}

function getStatusSeverity(status) {
    const statusMap = {
        'pending': 'warning',
        'approved': 'success',
        'rejected': 'danger'
    };
    return statusMap[status] || 'info';
}

function getStatusText(status) {
    const statusMap = {
        'pending': '待审核',
        'approved': '已通过',
        'rejected': '已拒绝'
    };
    return statusMap[status] || status;
}

function formatDate(dateString) {
    if (!dateString) return '';
    const date = new Date(dateString);
    return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
    });
}

async function submitForm() {
    try {
        loading.value = true;
        errorMsg.value = '';

        const response = await ApiService.post('employer/submit', formData);
        
        if (response.data.success) {
            app.flashMsg('申请提交成功', response.data.message || '请等待审核', 'success');
            router.push('/');
        } else {
            errorMsg.value = response.message || '申请提交失败，请稍后重试';
        }
    } catch (error) {
        console.error('Application error:', error);
        errorMsg.value = error.response?.data?.message || '申请提交失败，请稍后重试';
    } finally {
        loading.value = false;
    }
}

onMounted(() => {
    if (typeof window.particlesJS !== 'undefined') {
        window.particlesJS('particles-js', {
            particles: {
                number: {
                    value: 100,
                    density: {
                        enable: true,
                        value_area: 800
                    }
                },
                color: {
                    value: ['#4d7cfe', '#6c63ff', '#3ae374']
                },
                shape: {
                    type: 'circle',
                    stroke: {
                        width: 0,
                        color: '#000000'
                    }
                },
                opacity: {
                    value: 0.6,
                    random: true,
                    anim: {
                        enable: true,
                        speed: 1,
                        opacity_min: 0.1,
                        sync: false
                    }
                },
                size: {
                    value: 4,
                    random: true,
                    anim: {
                        enable: true,
                        speed: 2,
                        size_min: 0.1,
                        sync: false
                    }
                },
                line_linked: {
                    enable: true,
                    distance: 150,
                    color: '#4d7cfe',
                    opacity: 0.4,
                    width: 1
                },
                move: {
                    enable: true,
                    speed: 3,
                    direction: 'none',
                    random: true,
                    straight: false,
                    out_mode: 'out',
                    bounce: false,
                    attract: {
                        enable: true,
                        rotateX: 600,
                        rotateY: 1200
                    }
                }
            },
            interactivity: {
                detect_on: 'canvas',
                events: {
                    onhover: {
                        enable: true,
                        mode: 'grab'
                    },
                    onclick: {
                        enable: true,
                        mode: 'push'
                    },
                    resize: true
                },
                modes: {
                    grab: {
                        distance: 180,
                        line_linked: {
                            opacity: 0.8
                        }
                    },
                    push: {
                        particles_nb: 6
                    }
                }
            },
            retina_detect: true
        });
    } else {
        console.warn('particles.js is not loaded');
    }
});
</script>

<style lang="scss">
:root {
    --primary: #4d7cfe;
    --secondary: #6c63ff;
    --accent: #3ae374;
    --light: #f9fafe;
    --dark: #2c3e50;
    --gradient: linear-gradient(135deg, #6c63ff, #4d7cfe);
}

.login-page {
    min-height: 100vh;
    position: relative;
    overflow: hidden;
}

.login-background {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
    pointer-events: none;
}

.animated-shapes {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.shape {
    position: absolute;
    background: linear-gradient(45deg, var(--primary), var(--secondary));
    border-radius: 50%;
    opacity: 0.1;
    animation: float 15s infinite ease-in-out;

    &.shape1 {
        width: 300px;
        height: 300px;
        top: -150px;
        left: -150px;
        animation-delay: 0s;
    }

    &.shape2 {
        width: 200px;
        height: 200px;
        top: 50%;
        right: -100px;
        animation-delay: -5s;
    }

    &.shape3 {
        width: 150px;
        height: 150px;
        bottom: -75px;
        left: 50%;
        animation-delay: -10s;
    }
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    padding: 2rem;
    position: relative;
    z-index: 2;
    transform: translateY(0);
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    }
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;

    .logo-icon {
        width: 60px;
        height: 60px;
        background: var(--gradient);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto;
        color: white;
        font-size: 24px;
        font-weight: bold;
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

.form-group {
    margin-bottom: 1.5rem;
}

.login-input {
    border-radius: 10px;
    border: 1px solid #e0e0e0;
    transition: all 0.3s ease;

    &:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 2px rgba(77, 124, 254, 0.2);
    }
}

.login-button {
    background: var(--gradient);
    border: none;
    border-radius: 10px;
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0) rotate(0deg);
    }
    50% {
        transform: translateY(-20px) rotate(5deg);
    }
}

@media screen and (max-width: 768px) {
    .login-card {
        margin: 1rem;
        padding: 1.5rem;
    }

    .shape {
        opacity: 0.05;
    }
}

.check-status-btn {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    margin-right: 0.5rem;
    background: var(--primary);
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(77, 124, 254, 0.3);

    &:hover {
        background: var(--secondary);
        transform: translateY(-50%) scale(1.05);
        box-shadow: 0 4px 12px rgba(77, 124, 254, 0.4);
    }

    &:active {
        transform: translateY(-50%) scale(0.95);
    }

    .p-button-icon {
        margin-right: 0.5rem;
    }
}

.p-input-icon-right {
    position: relative;
    
    .p-inputtext {
        padding-right: 8rem;
    }
}

.status-tip {
    background: rgba(77, 124, 254, 0.1);
    border-left: 4px solid var(--primary);
    padding: 0.75rem 1rem;
    border-radius: 4px;
    color: var(--primary);
    font-weight: 500;
    display: flex;
    align-items: center;
    
    i {
        font-size: 1.2rem;
    }
}
</style> 